//
//  RecAudioListCell.swift
//

import UIKit

class RecAudioListCell: UITableViewCell {

    lazy var countLabel: UILabel = {
        let view = UILabel()
        view.textColor = UIColor.gray
        view.font = 15.font
        return view
    }()
    
    private lazy var titleLabel: UILabel = {
        let view = UILabel()
        view.textColor = UIColor.black
        view.font = 14.font
        return view
    }()
    
    private lazy var timeImageView: UIImageView = {
        UIImageView(image: "blh_albumlist_duration".image)
    }()
    
    private lazy var timeLabel: UILabel = {
        let view = UILabel()
        view.textColor = UIColor.gray
        view.font = 12.font
        return view
    }()
    
    private lazy var dateLabel: UILabel = {
        let view = UILabel()
        view.textColor = UIColor.gray
        view.font = 12.font
        return view
    }()
    
    private lazy var playerStateImageView: UIImageView = {
        let view = UIImageView()
        view.image = "icon_coursePlay_normal".image
        return view
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        addSubviews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func addSubviews() {
        
        contentView.addSubview(countLabel)
        contentView.addSubview(titleLabel)
        contentView.addSubview(timeImageView)
        contentView.addSubview(timeLabel)
        contentView.addSubview(dateLabel)
        contentView.addSubview(playerStateImageView)
        
        countLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(space)
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview().offset(-10.fit)
            make.left.equalTo(countLabel.snp.right).offset(space)
        }
        
        timeImageView.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview().offset(10.fit)
            make.left.equalTo(countLabel.snp.right).offset(space)
        }
        
        timeLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(timeImageView.snp.centerY)
            make.left.equalTo(timeImageView.snp.right).offset(3.fit)
        }
        
        dateLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(timeImageView.snp.centerY)
            make.left.equalTo(timeLabel.snp.right).offset(space)
        }
        
        playerStateImageView.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-space)
        }
    }
    
    func update(_ model: AudioItemModel) {
        titleLabel.text = model.title
        if let time = Double(model.duration) {
            timeLabel.text = time.formatTime
        }
        dateLabel.text = model.update_time.prefix("T")
        titleLabel.textColor = model.isSelected ? .theme : .black
        playerStateImageView.image = model.isSelected ? "icon_coursePlay_last".image : "icon_coursePlay_normal".image
    }
}
